package com.bear.atlassianmcp.jira.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

/**
 * Jira Attachment数据模型
 * 对应Jira REST API返回的Attachment对象
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class JiraAttachment {

    private String self;
    private String id;
    private String filename;
    private Map<String, Object> author;
    private String created;
    private Long size;
    private String mimeType;
    private String content;
    private String thumbnail;
    private Map<String, Object> properties;

    /**
     * 获取作者显示名称
     */
    public String getAuthorDisplayName() {
        return author != null ? (String) author.get("displayName") : null;
    }

    /**
     * 获取作者用户名
     */
    public String getAuthorName() {
        return author != null ? (String) author.get("name") : null;
    }

    /**
     * 获取格式化的文件大小
     */
    public String getFormattedSize() {
        if (size == null) {
            return "未知大小";
        }
        
        if (size < 1024) {
            return size + " B";
        } else if (size < 1024 * 1024) {
            return String.format("%.1f KB", size / 1024.0);
        } else if (size < 1024 * 1024 * 1024) {
            return String.format("%.1f MB", size / (1024.0 * 1024.0));
        } else {
            return String.format("%.1f GB", size / (1024.0 * 1024.0 * 1024.0));
        }
    }

    /**
     * 判断是否为图片文件
     */
    public boolean isImage() {
        return mimeType != null && mimeType.startsWith("image/");
    }

    /**
     * 判断是否有缩略图
     */
    public boolean hasThumbnail() {
        return thumbnail != null && !thumbnail.trim().isEmpty();
    }

    /**
     * 格式化为AI友好的文本描述
     */
    public String toFormattedString() {
        StringBuilder sb = new StringBuilder();
        sb.append("📎 附件: ").append(filename).append("\n");
        sb.append("ID: ").append(id).append("\n");
        sb.append("大小: ").append(getFormattedSize()).append("\n");
        sb.append("类型: ").append(mimeType != null ? mimeType : "未知").append("\n");
        sb.append("上传者: ").append(getAuthorDisplayName()).append("\n");
        sb.append("上传时间: ").append(created).append("\n");
        
        if (isImage() && hasThumbnail()) {
            sb.append("缩略图: 可用").append("\n");
        }
        
        if (content != null) {
            sb.append("下载链接: ").append(content).append("\n");
        }
        
        return sb.toString();
    }

    /**
     * 简化的格式化输出，用于列表显示
     */
    public String toSimpleString() {
        return String.format("📎 %s (%s) - %s", 
                           filename, 
                           getFormattedSize(), 
                           getAuthorDisplayName());
    }

    // Getters and Setters
    public String getSelf() {
        return self;
    }

    public void setSelf(String self) {
        this.self = self;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public Map<String, Object> getAuthor() {
        return author;
    }

    public void setAuthor(Map<String, Object> author) {
        this.author = author;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    public Map<String, Object> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, Object> properties) {
        this.properties = properties;
    }
}
